import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:im_demo/utils/logger_util.dart';

import '../../../../core/protos/v1/chat.pb.dart';
import '../../../constant/storage_key.dart';

class ProxySettingController extends GetxController {
  var enableProxy = false.obs;
  var addressEditController = TextEditingController();

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    _initProxy();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void _initProxy() {
    final bool enable = GetStorage().read(StorageKey.ENABLE_PROXY) ?? false;
    enableProxy.value = enable;
    final String proxyAddress =
        GetStorage().read(StorageKey.PROXY_ADDRESS) ?? '';
    updateAddressInputText(proxyAddress);
  }

  void onEnableProxyChange(bool? checked) async {
    logE('onEnableProxyChange()->checked:$checked');

    enableProxy.value = checked ?? !enableProxy.value;
    await GetStorage().write(StorageKey.ENABLE_PROXY, checked);
  }

  void updateAddressInputText(String text) {
    addressEditController.text = text;
  }

  void cleanAddressInputText() {
    addressEditController.clear();
  }

  void saveAddress(String text) async {
    if (text.isEmpty) {
      Get.snackbar('温馨提示', '请输入代理地址');
      return;
    }
    await GetStorage().write(StorageKey.PROXY_ADDRESS, text);

    Get.snackbar('温馨提示', '保存成功');
  }
}
